package com.fz.controller;

import com.alibaba.fastjson.JSON;
import com.fz.entity.Book;
import com.fz.entity.Shopingcar;
import com.fz.mapper.BookMapper;
import com.fz.mapper.ShopingcarMapper;
import com.fz.util.Dbutil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017/6/27.
 */
@Controller
public class ShopingCar {

    @RequestMapping(value = "/shoping",produces = {"application/json;charset=utf-8"}) @ResponseBody
    public String query(HttpServletRequest req, HttpServletResponse resp ) throws IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //PrintWriter out =resp.getWriter();
        Dbutil db = new Dbutil();

        SqlSession ss=db.getSession();

        ShopingcarMapper shop =ss.getMapper(ShopingcarMapper.class);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("name","王五");

        List<Shopingcar> list = shop.queryByName(map);
       // out.print(JSON.toJSONString(list));
        return JSON.toJSONString(list);
    }

    @RequestMapping("/shopingcar")
    public void query02(HttpServletResponse resp , HttpServletRequest res) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();

        Dbutil db = new Dbutil();
        SqlSession ss=db.getSession();
        BookMapper book = ss.getMapper(BookMapper.class);
        List<Book> list = book.query();
        //Book book1 = book.queryById(16);
        StringBuilder sbr = new StringBuilder();
        if(list.size()==0){
            sbr.append("<p><a href=\"#\">您的购物车是空的，立去购物</a></p>");
            out.print(sbr);
        }else {
            sbr.append("<div class=\"cart01\">");
            sbr.append("<table><tbody>\n" +
                    "<tr>\n" +
                    "    <th width=\"18\"><input type=\"checkbox\" class=\"allselect\" checked=\"\">&nbsp;</th><th class=\"cent\" width=\"60\">全选</th><th>商品名称</th>\n" +
                    "    <th width=\"80\">单价</th>\n" +
                    "    <th width=\"60\">数量</th>\n" +
                    "    <th width=\"90\">操作</th>\n" +
                    "</tr>");
            double totalPrice=0;
            for(Book book2 :list){
                int id= book2.getId();
                double om =book2.getPrice();
                String name =book2.getName();
                sbr.append("<tr>\n" +
                        "    <td><input type=\"checkbox\" value=\"\" checked=\"\"></td>");
                sbr.append(" <td><input type=\"hidden\" name=\"id\" value=\"\"></td>");
                sbr.append("<td><a href=\"#\">"+name+"</a></td>");
                sbr.append("<td><span>"+om+" ：元</span></td>");
                sbr.append("<td><span>"+1+"：件</span></td>");
                sbr.append("<td><a href=\"#\">删除</a></td></tr>");
            }
            sbr.append("</tbody>\n" +
                    "</table>");
            sbr.append("</div>");
            out.print(sbr);
        }


    }

    @RequestMapping("/delete.action") @ResponseBody
    public void delete(HttpServletRequest req, HttpServletResponse resp ) throws IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");


        Dbutil db = new Dbutil();
        SqlSession ss=db.getSession();
        ShopingcarMapper shop =ss.getMapper(ShopingcarMapper.class);

        String s =req.getParameter("id");
        System.out.println(s);
        int id =Integer.parseInt(s);

        int aa =shop.deleteById(id);
        ss.commit();
    }

    @RequestMapping("/update.action") @ResponseBody
    public void update(HttpServletRequest req, HttpServletResponse resp ) throws IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");


        Dbutil db = new Dbutil();
        SqlSession ss=db.getSession();
        ShopingcarMapper shop =ss.getMapper(ShopingcarMapper.class);

        String s =req.getParameter("id");
        int id =Integer.parseInt(s);

        String jia=req.getParameter("jia");
        String jian=req.getParameter("jian");

        String shpingcount=req.getParameter("count");
        int count=Integer.parseInt(shpingcount);

        System.out.println(s);
        System.out.println(jia);
        System.out.println(shpingcount);

        if(jia!=null){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("id",id);
            map.put("count",count+1);
            shop.updateById(map);
            ss.commit();
        }
        if(jian!=null&&count>0){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("id",id);
            map.put("count",count-1);
            shop.updateById(map);
            ss.commit();
        }


    }

}
